package com.lin.service.impl;
import com.lin.common.constant.MessageConstant;
import com.lin.common.exception.AccountException;
import com.lin.common.exception.BaseException;
import com.lin.common.exception.NotRegisterException;
import com.lin.common.exception.PasswordErrorException;
import com.lin.common.result.Result;
import com.lin.entity.Admin;
import com.lin.entity.User;
import com.lin.entity.dto.UserDto;
import com.lin.mapper.AdminMapper;
import com.lin.mapper.UserMapper;
import com.lin.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    // 使用spring-security进行密码加密
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);

    public User loginUser(UserDto userDto) {
        User existingUser = userMapper.findByEmailOrPhoneOrUsername(userDto);
        if (existingUser != null ) {
            //登录成功
            if (encoder.matches(userDto.getPassword(), existingUser.getPassword())){
                return existingUser;
            }
            //密码错误
            else {
                throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
            }
        }
        //未注册
        else
            throw new NotRegisterException(MessageConstant.ACCOUNT_NOT_FOUND);
    }
    public Admin loginAdmin(Admin admin) {
        Admin existingAdmin = adminMapper.findByUserName(admin.getUsername());
        if (existingAdmin != null ) {
            //登录成功
            if (admin.getPassword().equals(existingAdmin.getPassword())){
                return existingAdmin;
            }
            //密码错误
            else {
                throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
            }
        }
        //未注册
        else
            throw new NotRegisterException(MessageConstant.ACCOUNT_NOT_FOUND);
    }

    /**
     * 获取用户消息
     * @param userId
     * @return
     */
    public User findById(Long userId) {
        User user = userMapper.findByUserId(userId);
        return user;
    }

    /**
     * 验证码登录
     * @param userDto
     * @return
     */
    public User loginByToken(UserDto userDto) {
        User existingUser = userMapper.findByEmailOrPhoneOrUsername(userDto);
        if (existingUser == null) {
            throw new AccountException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //查询redis中的token
        String token = (String) redisTemplate.opsForValue().get(userDto.getPhone());
        //验证成功
        if (token == null) {
            throw new BaseException("验证码已过期，请重新发送");
        }
        else if (token.equals(userDto.getToken())) {
            return existingUser;
        } else
            throw new BaseException(MessageConstant.TOKEN_ERROR);
    }
}
